package com.etlmaster.transformer.funcs;

import java.util.Map;

import com.etlmaster.executor.annotation.StepInstruction;
import com.etlmaster.executor.trans.core.Line;
import com.etlmaster.executor.trans.core.LineTransProcessor;
import com.etlmaster.executor.utils.LogWriter;


/**
 * 
 * 定长分割
 * @author Larry
 * @copyright Larry@etlMaster
 * @since 2017-09-10
 */
@StepInstruction("fieldSplitBylength")
public class TransFieldSplit  extends LineTransProcessor {

	private String lengthsArray;
	private String []lengths;
	public TransFieldSplit(String sourceTab,String sourceCol,String targetTab,String targetCol,String changeCode,Map paramStr,Map var){
		
		super( sourceTab,sourceCol,targetTab,targetCol,changeCode, paramStr,var);
	
	
		lengths=lengthsArray.split(",");
	}
	
	@Override
	public void transField(Line line) {
		int begin = 0;
		String fieldValue=line.getField(getFieldIndex(line));
		for (int i = 0; i < lengths.length; i++) {
			int length=Integer.parseInt(lengths[i]);
			char[] dst = new char[(length)];
			try{
				fieldValue.getChars(begin, begin + length, dst, 0);
				line.addField(String.valueOf(dst).trim(), this.getSourceTab(),this.getSourceCol()+"_"+i)  ;
				begin += length;
			}catch(Exception e){
				line.setSkip(true);
				LogWriter.addLog("DEBUG","ignore line:{}",fieldValue);
				LogWriter.addLog( "DEBUG",e.getMessage());
			}
		}
		//line.deleteField(getFieldIndex(line));
	}

}
